package com.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.bussiness.PoiInterface;
import com.bussiness.domain.PoiTemplate;
import com.bussiness.domain.PoiTemplate.Type;
import com.bussiness.impl.PoiImpl;

/**
 * Servlet implementation class GetPoiTemplateServlet
 */
@WebServlet("/getpoitemp")
public class GetPoiTemplateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(GetPoiTemplateServlet.class);  
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetPoiTemplateServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PoiInterface poi = new PoiImpl();
		PoiTemplate template;
		long id = 1;

		
		try{
			String ids = (String) request.getParameter("id");
			id = Long.parseLong(ids);
			logger.info(id);
		}catch(Exception e){
			logger.error(e);
		}
		
		
		template = poi.readPoiTemplate(id);
		logger.info(template);

		//construct html snippet based on template
		Map<String, Type> props = template.getProps();
		request.setAttribute("props", props);

		
		RequestDispatcher view = request.getRequestDispatcher("WEB-INF/private/create.jsp");
		 view.forward(request, response);
	}

}
